package com.qf.boot2202day02.controller;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class UploadController {

    @Value("${myfile.dir}")
    private String fileDir;

    @PostMapping("/test1")
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {

        String filename = UUID.randomUUID().toString();
        String originalFilename = file.getOriginalFilename();
        String extension = FilenameUtils.getExtension(originalFilename);
        String finalFilePath = fileDir + File.separator + filename+"."+extension;
        file.transferTo(new File(finalFilePath));
        return "ok";
    }

    @GetMapping("/getdir")
    public String getuserDir(){
        String dir = System.getProperty("user.dir");
        return dir;

    }
}
